App Events
The AppEvents module in the Scripting app provides an interface to observe global application state changes, such as scene lifecycle transitions and changes in system-wide appearance (light/dark mode). These capabilities are essential for writing responsive scripts that react appropriately to runtime context.
Scene Lifecycle
ScenePhase
Represents the state of the app’s scene:
active– The app is in the foreground and interactive.inactive– The app is in transition or temporarily inactive.background– The app is running in the background and not visible.
Color Scheme
ColorScheme
Reflects the current appearance mode of the device:
light– Light mode UIdark– Dark mode UI
AppEventListenerManager<T>
A generic event manager that allows you to register and unregister listeners dynamically. Used for both scenePhase and colorScheme.
AppEvents Class
AppEvents.scenePhase
Listen for scene phase transitions. Ideal for responding to foreground/background state changes.
Example
AppEvents.colorScheme
Observe system-wide light/dark appearance changes in real time.
Example
useColorScheme() Hook
Description
This hook provides a reactive way to access the current ColorScheme ('light' or 'dark') within a component. It automatically updates the value when the system theme changes.
Example
Notes
- Listeners registered with
AppEventsshould be manually removed when no longer needed to prevent memory leaks. useColorScheme()is the recommended way to reactively reflect the current theme in your components.- These APIs allow you to respond to system and app-level state changes in a clean, declarative way—without relying on imperative lifecycle logic.
